//账单详情
'use strict';
import React from 'react';
import {
  View,
  Text,
  ListView,
  StyleSheet,
  TouchableOpacity,
  Alert,
} from 'react-native';
import NavBar from '../components/NavigatorBar';
import * as defaultStyles from '../constants/DefaultStyles';
import {Actions} from 'react-native-router-flux';
import BillDetailCell from '../components/BillDetailCell';
import KsiBarcode from "react-native-ksi-barcode";
import { requestXML } from '../utils/RequestUtils';
import { json2xml_str } from '../utils/XmlUtils';
import Spinner from 'react-native-loading-spinner-overlay';
import {tradeState} from '../constants/TradeState';

export default class BillDetailContainer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLoading: false,
      transtat: props.transtat,
      isSync: false,
    }
  }
  _syncState() {
    this.setState({isLoading: true});
    this.state.isSync = true;
    let xml = json2xml_str(JSON.stringify({
      Request: {
        Head: {
          trace_no: '6',
          terminalno: '8',
          acceptidcode: merid,
          tanstype: this.props.paymentType === 'svcP' ? '80004' : '80010',
          operateno: '3'
        },
        Body: {
          outtradeno: this.props.outtradeno,
          SIGN: '122',
        }}
    }));
    const promise = requestXML('http://192.168.1.112:8080/icmp-ums/dhc/sys/mobile.do', xml, 20000);
    promise.then((object) => {
        console.log(object);
        let respcd = '-1';
        try {
          let Body = object.Response.Body;
          respcd = Body.respcd;
          if (respcd === '00') {
            this.setState({isLoading: false, transtat: Body.transtat})
          }
          else {
            Alert.alert('提示', '同步失败', [{text: '确定', onPress: () => {this.setState({isLoading: false})}}]);
            return;
          }
        }
        catch (err) {
          Alert.alert('提示', '返回数据格式出错', [{text: '确定', onPress: () => {this.setState({isLoading: false})}}]);
        }

      },(reject) => {
        try{
          if (reject.state === "-1") {
            Alert.alert('提示', '请求超时，请检查网络连接', [{text: '确定', onPress: () => {this.setState({isLoading: false})}}]);
            return;
          }
        }
        catch (e) {
          Alert.alert('提示', '请求失败，请检查网络连接', [{text: '确定', onPress: () => {this.setState({isLoading: false})}}]);
        }
      }
    );
  }
  _isbtnHiden(transtat: String) {
    switch(transtat) {
      // case 'ST00':
      //   return true;
      // case 'ST10':
      //   return true;
      default:
        return false;
    }
  }
  render() {
    let payment = {
      'svcW': '微信单号',
      'svcP': '支付宝单号',
    };
    let paymentName = {
      'svcW': '微信-公众号支付',
      'svcP': '支付宝支付',
    }
    return (
      <View style={styles.container}>
        <Spinner visible={this.state.isLoading} textContent="同步中"
                 textStyle={{color: 'white'}}  overlayColor={'rgba(0, 0, 0, 0.7)'}/>
        <NavBar title="交易详情" leftImage="angle-left" navBarColor={defaultStyles.LIGHTBLUE}
                leftClick={() => {
                  Actions.pop();
                  if (!this._isbtnHiden(this.state.transtat) && this.state.isSync) {
                    this.props.back(!this._isbtnHiden(this.state.transtat));
                  }
                }}/>
        <BillDetailCell leftText="收款金额" rightText={this.props.receiptMoney}
                        fontSize={18} style={{height: 55}}/>
        <BillDetailCell leftText="交易时间" rightText={this.props.time}
                        fontSize={14} />
        <BillDetailCell leftText="支付方式" rightText={paymentName[this.props.paymentType]}
                        fontSize={14} />
        <BillDetailCell leftText={payment[this.props.paymentType]} rightText={this.props.transid}
                        fontSize={14} style={{height: this.props.transid ? 30 : 0}}/>
        <BillDetailCell leftText="交易单号" rightText={this.props.outtradeno}
                        fontSize={14} />
        <BillDetailCell leftText="交易状态" rightText={tradeState(this.state.transtat)}
                        fontSize={14} rightTextColor={this.state.transtat === 'ST00' ? 'green' : 'gray'}/>
        <View style={{width: defaultStyles.SCREENWIDTH, height: 100, alignItems: 'center',
        backgroundColor: 'white'}}>
          <KsiBarcode text={this.props.outtradeno} style={{fontSize: 60, marginTop: 10}}/>
          <Text style={{marginTop: 5, textAlign: 'center'}}>{this.props.outtradeno}</Text>
        </View>
        <TouchableOpacity style={{height: this._isbtnHiden(this.state.transtat) ? 0 : 40, backgroundColor: 'white', borderRadius: 5,
        margin: 15,  justifyContent: 'center'}}
                          onPress={() => this._syncState()} disabled={this._isbtnHiden(this.state.transtat)}>
          <Text style={{textAlign: 'center'}}>同步订单状态</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex:1,
    backgroundColor: defaultStyles.WHITEOFBACKGROUND,
  }

});